using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using NovodexWrapper;

/// <summary>
/// this is the model that represents the goal or target to build towards
/// </summary>
/// 
namespace marbletrack
{
    public class EndOfTrack:Obstacle
    {
        protected MouseSquare square;

        public EndOfTrack()
        {
            do
            {
                modelPosition_ = nextModelPosition_ = new Vector3(
                    (float)(Game1.Ran.Next(-10000, 10000)),
                    (float)(Game1.Ran.Next(0, 5000)),
                    (float)(Game1.Ran.Next(-10000, 10000)));
            } while (modelPosition_.X != Game1.StartingPosition.X && modelPosition_.Z != Game1.StartingPosition.Z);
            
    // Setup Variables with default values
            SetupVariables(Vector3.UnitX * 180, modelPosition_);
        }

        public EndOfTrack(Vector3 modelPosition)
        {
            // Setup Variables with specified values
            SetupVariables(Vector3.UnitX * 180, modelPosition);
        }

        // Setup Variables
        internal override void SetupVariables(Vector3 rotation, Vector3 modelPosition)
        {
           active = true;
            
            //active = true;
            modelScale_ = 1f;
            nextModelPosition_ = modelPosition_ = modelPosition;
            square = new MouseSquare(modelPosition_ - Vector3.UnitY * 101, Game1.metal1);
            // Setup orientation variables
            modelRotation_ = rotation;
            // Load model
            model_ = Game1.EndPiece;
            CorrectPosition_ = Matrix.CreateTranslation(new Vector3(Game1.SQUARESIZE * 0f, Game1.SQUARESIZE * -2f, Game1.SQUARESIZE * 0f));
            transformMatrix_ = ApplyTransformations();
            // Create and Load physics actor
            //trackActor_ = physics.CreateTriangleMeshActor(model_.MeshVertexBuffers[0], model_.MeshIndexBuffers[0], transformMatrix_, Physics.TRACKPIECE_PHYSICS_GROUP, true, model_.FlipNormalsFlag);
            model_.LoadTexture(texture);
        }

        public Vector3 ModelPosition//for loading from save
        {
            set
            {
                nextModelPosition_ = modelPosition_ = value;
                square.Position = modelPosition_ - Vector3.UnitY * 101;
            }
        }

        public override void Update()
        {
            CorrectAngle(ref modelRotation_.Y);
            //MakeItFloat(modelRotation_.Y);
            //CreateMovement(1f);
            //used as an update to check if track is complete
            if (Vector3.Distance(Game1.LastTrackPiece.exitPoint_, modelPosition_) < Game1.SQUARESIZE * 1f && !Game1.trackComplete)
            {
                //CreateRotationY(5);
                Game1.trackComplete = true;
                Game1.cameraBall.Position = Game1.cameraBall.StartingPosition;
            }
            else
            {
                CreateRotationY(1);
                //Game1.trackComplete = false;
            }
        }

        public override void Draw()
        {
            transformMatrix_ = ApplyTransformations();
            model_.Draw(transformMatrix_);
            Game1.basicEffect.DiffuseColor = Color.Red.ToVector3();
            Game1.basicEffect.TextureEnabled = false;
            square.Draw();
            Game1.basicEffect.TextureEnabled = true;
            Game1.basicEffect.DiffuseColor = Color.White.ToVector3();
        }
    }
}
